package com.ipharma.assignments;

import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

import com.ipharma.beans.BeansException;
import com.ipharma.beans.Helper;
import com.ipharma.config.Config;
import com.ipharma.config.Session;
import com.ipharma.model.Asignacion;
import com.ipharma.model.OpcionesAsignacion;
import com.ipharma.model.Paciente;
import com.ipharma.service.ServiceException;
import com.ipharma.service.TransaccionService;

/**
 * A Assignment class.
 * <P>
 * 
 * @author JorgE.H
 */
public class Assignment extends Object {

   /**
    * Attributes
    */
   int astId = -1;

   String astDescription = "";

   int pttId = -1;

   String pttCode = "";

   String pttFirstname = "";

   String pttLastname = "";

   String aonName = "";

   String aonDescription = "";
   
   String tipo = "";

   /**
    * Constructor
    * 
    * @param pAstId
    *           assignment id
    * @param pAstDescription
    *           assignment description
    * @param pPttId
    *           patient id
    * @param pPttCode
    *           patient code
    * @param pPttFirstname
    *           patient firstname
    * @param pPttLastname
    *           patient lastname
    */
   public Assignment(int pAstId, String pAstDescription, int pPttId,
         String pPttCode, String pPttFirstname, String pPttLastname) {

      astId = pAstId;
      astDescription = pAstDescription;
      pttId = pPttId;
      pttCode = pPttCode;
      pttFirstname = pPttFirstname;
      pttLastname = pPttLastname;
      tipo = "paciente";
   }

   /**
    * Constructor
    * 
    * @param pAstId
    *           assignment id
    * @param pAstDescription
    *           assignment description
    * @param pAonName
    *           assignment option name
    * @param pAonDescription
    *           assignment option description
    */
   public Assignment(int pAstId, String pAstDescription, String pAonName,
         String pAonDescription) {

      astId = pAstId;
      astDescription = pAstDescription;
      aonName = pAonName;
      aonDescription = pAonDescription;
      tipo = "otro";
   }

   /**
    * Returns an array of string with assignments
    * 
    * @param pId
    *           the Assignment id
    */
   public static Assignment[] getAssignments(Session pSess, int pType, Config config)
         throws SQLException {

      try {
         TransaccionService dbAst = Helper.getTransaccionService();
         List l_rs;
         Assignment[] la_assigns = null;
         int i = 0, li_qty5 = 0;
         
         Asignacion asignacion = dbAst.findById(new Long(pType));
         if (asignacion != null) {
            String ls_astDesc = asignacion.getDescripcion();
            if (pType == config.getAssignmentPaciente()) {
               
               l_rs = dbAst.findPacientes();
               la_assigns = new Assignment[l_rs.size()];
               for (Iterator iter = l_rs.iterator(); iter.hasNext();) {
                  Paciente paciente = (Paciente) iter.next();
                  String ls_first = paciente.getNombre();
                  String ls_last = paciente.getApellido();
                  String ls_code = paciente.getCodigo();
                  int li_id = paciente.getId().intValue();

                  la_assigns[i++] = new Assignment(pType, ls_astDesc, li_id,
                        ls_code, ls_first, ls_last);
               }
            } else {
               l_rs = dbAst.findOpcionesAsignacion(new Long(pType), "", "");
               la_assigns = new Assignment[l_rs.size()];
               for (Iterator iter = l_rs.iterator(); iter.hasNext();) {
                  OpcionesAsignacion opcion = (OpcionesAsignacion) iter.next();
                  String ls_name = opcion.getNombre();
                  String ls_description = opcion.getDescripcion();

                  la_assigns[i++] = new Assignment(pType, ls_astDesc, ls_name,
                        ls_description);
               }
            }
         }
         return la_assigns;
      } catch (RemoteException e) {
         e.printStackTrace();
      } catch (BeansException e) {
         e.printStackTrace();
      } catch (ServiceException e) {
         e.printStackTrace();
      }
      return null;
   }

   /**
    * Returns assignment type id
    * 
    * @return the assignment type id
    */
   public int getAstId() {

      return astId;
   }

   /**
    * Returns assignment type description
    * 
    * @return the assignment type description
    */
   public String getAstDescription() {

      return astDescription;
   }

   /**
    * Returns patient id
    * 
    * @return the patient id
    */
   public int getPttId() {

      return pttId;
   }

   /**
    * Returns patient code
    * 
    * @return the patient code
    */
   public String getPttCode() {

      return pttCode;
   }

   /**
    * Returns patient first name
    * 
    * @return the patient first name
    */
   public String getPttFirstname() {

      return pttFirstname;
   }

   /**
    * Returns patient lastname
    * 
    * @return the patient lastname
    */
   public String getPttLastname() {

      return pttLastname;
   }

   /**
    * Returns assignment option name
    * 
    * @return the assignment option name
    */
   public String getAonName() {

      return aonName;
   }

   /**
    * Returns assignment option description
    * 
    * @return the assignment option description
    */
   public String getAonDescription() {

      return aonDescription;
   }

   /**
    * Overrides object method
    * 
    * @return the string representation of current object
    */
   public String toString() {

      if (tipo.equals("paciente"))
         return pttLastname + " " + pttFirstname;
      else
         return aonDescription;
   }

}
